<?php
//
defined('BY_JYA') or exit('error');
//cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	if(intval($_RQ['store_id'])){
		$store_id = intval($_RQ['store_id']);
		$this->session('store_id',$store_id);
	}
	if(!$store_id){
		$store_id=$this->session('store_id');
	}
	if(!$store_id){
		exi('请先选择场馆！','error','jy_qiuyuyue/index/');
	}
	$order_type=$this->session('order_type');
	$carts=$this->session('carts');
	$table_id=$this->session('table_id');
	$table_list=pdo_getall('jy_qiuyuyue_table',array('store_id'=>$store_id,'busy'=>0,'status'=>1));
	$store=pdo_get('jy_qiuyuyue_store',array('id'=>$storeid));
	$return['order_type']=$order_type;
	$return['table_id']=$table_id;
	$return['carts']=$carts;
	$return['table_list']=$table_list;
	set_html('title','堂点订单');
	$return['store']=$store;
	exi($return);
}
	//提交订单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择场馆！','error','jy_qiuyuyue/index/');
	}	
	$store=pdo_get('jy_qiuyuyue_store',array('id'=>$store_id));
	$carts=$this->session('carts');
	if(!$carts){
		exi('购物车为空','error');
	}
	$total=0;
	foreach($carts as $k=>$v){
			$price=pdo_getcolumn('jy_qiuyuyue_goods',array('id'=>$k),'price');
			$total=$price*$v + $total;
		}
	if($this->session('order_type')==1){
		$table=pdo_get('jy_qiuyuyue_table',array('id'=>$this->session('table_id')));
		if($table['busy']==1){
			exi('餐桌已被占用，请重新选择餐桌','error');
		}
		$price=$store['in_pay']*$post['user_num']+$total;	
		$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'mid'=>$_SESSION['uid'],
		'order_type'=>1,
		'ordersn'=>get_ordersn('jy_qiuyuyue_order','ordersn'),
		'price'=>$price,
		'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_qiuyuyue_order',$data);
		$this->session('insertid',pdo_insertid());
		$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'user_num'=>$post['user_num'],
		'table_id'=>$this->session('table_id'),
		'order_id'=>pdo_insertid()
		);
		pdo_insert('jy_qiuyuyue_order_table',$data);
	}
	if($this->session('order_type')==2){
		
		$lng=json_decode($store['lat_lng'],true);
		$out_distance=$store['out_distance'];
		
		$user_lng=$post['receiver_latng'];
		if($user_lng){
			$distance=cfc('tools')->getdistance($lng['lng'],$lng['lat'],$user_lng['lng'],$user_lng['lat']);
			if($distance>$out_distance){
				exi('超出配送距离','error');
			}
		}
		if($store['out_end']<$total){
		$price=$total;
		}else{
		$price=$total+$store['out_pay'];	
		}
		$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'mid'=>$_SESSION['uid'],
		'order_type'=>2,
		'ordersn'=>get_ordersn('jy_qiuyuyue_order','ordersn'),
		'price'=>$price,
		'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_qiuyuyue_order',$data);
		$this->session('insertid',pdo_insertid());
		$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'order_id'=>pdo_insertid(),
		'receiver_name'=>$post['receiver_name'],
		'receiver_mobile'=>$post['receiver_mobile'],
		'receiver_address'=>$post['receiver_address'],
		'receiver_latng'=>$post['receiver_latng'],
		);
		pdo_insert('jy_qiuyuyue_order_out',$data);
	}
	if($this->session('order_type')==3){
		$price=$total;
		$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'mid'=>$_SESSION['uid'],
		'order_type'=>3,
		'ordersn'=>get_ordersn('jy_qiuyuyue_order','ordersn'),
		'price'=>$price,
		'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_qiuyuyue_order',$data);
		$this->session('insertid',pdo_insertid());
	}
	$str='';
	foreach($carts as $k=>$v){
			$good=pdo_get('jy_qiuyuyue_goods',array('id'=>$k));
			$str .=$good['title'].'*'.$v.'/n';
			$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'order_id'=>$this->session('insertid'),
			'goods_id'=>$k,
			'goods_num'=>$v,
			'goods_price'=>$good['price'],			
			'goods_title'=>$good['title']
			);
			pdo_insert('jy_qiuyuyue_order_goods',$data);
		}
	$this->session('carts','');
	$nickname=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname');
	$order_type=$this->type__order()[$this->session('order_type')];
	$data=array(
		'first'=>array('value'=>'订单付款通知','color'=>''),
		'tradeDateTime'=>array('value'=>'下单时间:'.date('Y-m-d h:i:s',TIMESTAMP),'color'=>'#ff510'),
		'orderType'=>array('value'=>'订单类型:'.$order_type,'color'=>'#ff510'),
		'customerInfo'=>array('value'=>'下单人'.$nickname,'color'=>'#ff510'),
		'orderItemName'=>array('value'=>'商品详情','color'=>'#ff510'),
		'orderItemData'=>array('value'=>'商品详情内容'.$str,'color'=>'#ff510'),
		'remark'=>array('value'=>'其他信息','color'=>'#ff510'),
	);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_qiuyuyue/order_show/',true);
	cfc('account')->sendTplNotice($_SESSION['uid'],$tpl['order_new'],$data,$url,'#980000');
    $params['fee']     = $price;
    $params['title']   = '外卖订单';
	$params['id']=$this->session('insertid');
	$params['plugin']='jy_qiuyuyue';
	$params['back_url']='jy_qiuyuyue/order/payresult';
	cfc('core')->pay($params);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];
	
	if($payresult['status']==1){
		$order=pdo_get('jy_qiuyuyue_order',array('ordersn'=>$payresult['id']));
		if($order['price']==$payresult['fee']){
			pdo_update('jy_qiuyuyue_order',array('pay_type'=>$payresult['pay_type'],'is_pay'=>1,'pay_id'=>$payresult['pay_id']),array('ordersn'=>$payresult['id']));
			$tpl=$this->plugin_setting('message_tpl');
			$url=custom_url('jy_qiuyuyue/order_show/',true);
			$store=pdo_getcolumn('jy_qiuyuyue_store',array('id'=>$order['store_id']),'title');
			$data=array(
				'first'=>array('value'=>'订单付款通知','color'=>''),
				'keyword1'=>array('value'=>'订单编号:'.$payresult['id'],'color'=>'#ff510'),
				'keyword2'=>array('value'=>'消费金额:'.$payresult['fee'],'color'=>'#ff510'),
				'keyword3'=>array('value'=>'消费场馆:'.$store,'color'=>'#ff510'),
				'keyword4'=>array('value'=>'消费时间:'.$order['createtime'],'color'=>'#ff510'),
				'remark'=>array('value'=>'更多消息请查看订单详情','color'=>'#ff510'),
			);
			cfc('account')->sendTplNotice($order['mid'],$tpl['order_pay'],$data,$url,'#980000');
			exi('支付成功','success','jy_qiuyuyue/order/');
		}else{
			exi('支付失败','error','jy_qiuyuyue/order/');
		}
	}else{
		exi('支付失败','error','jy_qiuyuyue/order/');
	}
}
?>